package com.antilia.infomaresme;

import org.apache.wicket.RuntimeConfigurationType;
import org.apache.wicket.Session;
import org.apache.wicket.guice.GuiceComponentInjector;
import org.apache.wicket.injection.Injector;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.request.Request;
import org.apache.wicket.request.Response;
import org.apache.wicket.request.resource.ResourceReference;
import org.odlabs.wiquery.ui.themes.IThemableApplication;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

import com.antilia.infomaresme.config.modules.Module;
import com.antilia.infomaresme.data.dao.interfaces.ICategoriesDao;
import com.antilia.infomaresme.data.dao.interfaces.ICategoryAlertsDao;
import com.antilia.infomaresme.data.dao.interfaces.IImageInfosDao;
import com.antilia.infomaresme.data.dao.interfaces.IItemEventsDao;
import com.antilia.infomaresme.data.dao.interfaces.IItemNewsDao;
import com.antilia.infomaresme.data.dao.interfaces.IItemsDao;
import com.antilia.infomaresme.data.dao.interfaces.IUserItemAlertsDao;
import com.antilia.infomaresme.data.dao.interfaces.IUserMessagesDao;
import com.antilia.infomaresme.data.dao.interfaces.IUsersDao;
import com.antilia.infomaresme.data.services.IAuthenticator;
import com.antilia.infomaresme.data.services.IImageService;
import com.antilia.infomaresme.jobs.impl.UserItemsAlertJob;
import com.antilia.infomaresme.jobs.quartz.QuartzJobExecuter;
import com.antilia.infomaresme.pages.HomePage;
import com.antilia.infomaresme.pages.home.PrivateSearchPage;
import com.antilia.infomaresme.pages.signin.LogInPage;
import com.antilia.infomaresme.pages.testmaps.Geocoder;
import com.antilia.infomaresme.resources.jqui.Style;
import com.google.inject.Guice;
import com.google.inject.Inject;


/**
 * Application object for your web application. If you want to run this application without deploying, run the Start class.
 * 
 * @see com.wiquery.plugins.test.Start#main(String[])
 */
public class InfomaresmeApplication extends WebApplication implements IThemableApplication
{    
	
	private static DisableProtectedAuthorizationStrategy AUTORIZATION_STRATEGY; 

	private boolean isDev = "DEVELOPMENT".equals(System.getProperty("com.antilia.test"));
	
	public static final String GMAPKey = "ABQIAAAA26YOe8DNWP0oaoBqjz97YRRLbqpOOl0269ekpMetksHEzQe-7xQJoo9YdxNPjyJ0Py278lOa6wWLyA";
	
	@Inject
	private IAuthenticator authenticator;
	
	@Inject
	private ICategoriesDao categoriesDao;
	
	@Inject
	private IItemsDao iItemsDao;
	
	@Inject
	private IUserMessagesDao userMessagesDao;
	
	@Inject
	private IUsersDao usersDao;
	
	@Inject
	private IImageInfosDao imageInfosDao;
	
	@Inject
	private IImageService imageService;
	
	@Inject
	private IItemNewsDao iItemNewsDao;
	
	@Inject
	private IItemEventsDao eventsDao;
	
	@Inject
	private IUserItemAlertsDao alertsDao;
	
	@Inject
	private ICategoryAlertsDao categoryAlertsDao;
	
	private Scheduler scheduler;
	
	public static com.google.inject.Injector injector = Guice.createInjector(new Module());
	
	private Geocoder geocoder = new Geocoder(GMAPKey);

    /**
     * Constructor
     */
	public InfomaresmeApplication()
	{
	}
	
	/**
	 * @see org.apache.wicket.Application#getHomePage()
	 */
	public Class<? extends WebPage> getHomePage()	{
		if(InfomaresmeSession.getSession().isAuthenticated())
			return PrivateSearchPage.class;
		return com.antilia.infomaresme.pages.HomePage.class;
		
		//return EditItemPage.class;
	}
	
	@Override
	protected void init() {
		super.init();
		AUTORIZATION_STRATEGY = new DisableProtectedAuthorizationStrategy() {
			
			public java.lang.Class<? extends org.apache.wicket.markup.html.WebPage> getSignInPage() {
				return LogInPage.class;
			};
		};
	

          // and start it off
          
		
		getSecuritySettings().setUnauthorizedComponentInstantiationListener(AUTORIZATION_STRATEGY);
		getSecuritySettings().setAuthorizationStrategy(AUTORIZATION_STRATEGY);
		getApplicationSettings().setAccessDeniedPage(LogInPage.class);
		getApplicationSettings().setPageExpiredErrorPage(HomePage.class);
		
		getDebugSettings().setOutputMarkupContainerClassName(false);
		getMarkupSettings().setStripWicketTags(true);		
		getComponentInstantiationListeners().add(new GuiceComponentInjector(this, injector));
        Injector.get().inject(this);	
        
        try {
			scheduler = StdSchedulerFactory.getDefaultScheduler();
			scheduler.start();
			JobDataMap dataMap = new JobDataMap();
			dataMap.put(QuartzJobExecuter.IJob_ID, new UserItemsAlertJob());
			JobDetail jobDetail = JobBuilder.newJob(QuartzJobExecuter.class)
			 .withIdentity("job1", "group1").usingJobData(dataMap)
            .build();			
			Trigger trigger =  TriggerBuilder.newTrigger()
			.forJob(jobDetail).startNow()
			.withSchedule(SimpleScheduleBuilder.simpleSchedule()
                    .withIntervalInSeconds(500)
                    .repeatForever()).build();
			scheduler.scheduleJob(jobDetail, trigger);
			
		} catch (SchedulerException e) {

		}
		
		///getDebugSettings().setAjaxDebugModeEnabled(true);
	}


	 
	public static InfomaresmeApplication getQAApplication() {
		return (InfomaresmeApplication)WebApplication.get();
	}

	
	@Override
	public Session newSession(Request request, Response response) {
		return new InfomaresmeSession(request);
	}

	
	
	protected boolean isInDevelopmentMode() {
		return isDev;
	}
	
	@Override
    public RuntimeConfigurationType getConfigurationType() {
        return (isDev ? RuntimeConfigurationType.DEVELOPMENT: RuntimeConfigurationType.DEPLOYMENT);
    }

	public IAuthenticator getAuthenticator() {
		return authenticator;
	}

	public void setAuthenticator(IAuthenticator authenticator) {
		this.authenticator = authenticator;
	}

	public ICategoriesDao getCategoriesDao() {
		return categoriesDao;
	}

	public void setCategoriesDao(ICategoriesDao categoriesDao) {
		this.categoriesDao = categoriesDao;
	}

	public IItemsDao getiItemsDao() {
		return iItemsDao;
	}

	public void setiItemsDao(IItemsDao iItemsDao) {
		this.iItemsDao = iItemsDao;
	}

	public IUsersDao getUsersDao() {
		return usersDao;
	}

	public void setUsersDao(IUsersDao usersDao) {
		this.usersDao = usersDao;
	}

	public ResourceReference getTheme(Session session) {
		return Style.CSS;
	}

	public IImageInfosDao getImageInfosDao() {
		return imageInfosDao;
	}

	public void setImageInfosDao(IImageInfosDao imageInfosDao) {
		this.imageInfosDao = imageInfosDao;
	}

	public IImageService getImageService() {
		return imageService;
	}

	public void setImageService(IImageService imageService) {
		this.imageService = imageService;
	}

	public IItemNewsDao getiItemNewsDao() {
		return iItemNewsDao;
	}

	public void setiItemNewsDao(IItemNewsDao iItemNewsDao) {
		this.iItemNewsDao = iItemNewsDao;
	}

	public IItemEventsDao getEventsDao() {
		return eventsDao;
	}

	public void setEventsDao(IItemEventsDao eventsDao) {
		this.eventsDao = eventsDao;
	}

	public IUserItemAlertsDao getAlertsDao() {
		return alertsDao;
	}

	public void setAlertsDao(IUserItemAlertsDao alertsDao) {
		this.alertsDao = alertsDao;
	}

	public ICategoryAlertsDao getCategoryAlertsDao() {
		return categoryAlertsDao;
	}

	public void setCategoryAlertsDao(ICategoryAlertsDao categoryAlertsDao) {
		this.categoryAlertsDao = categoryAlertsDao;
	}

	public IUserMessagesDao getUserMessagesDao() {
		return userMessagesDao;
	}

	public void setUserMessagesDao(IUserMessagesDao userMessagesDao) {
		this.userMessagesDao = userMessagesDao;
	}

	public Geocoder getGeocoder() {
		return geocoder;
	}

	public void setGeocoder(Geocoder geocoder) {
		this.geocoder = geocoder;
	}

}


